package com.sky;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : wushikai
 * <p>
 * date : 2022-07-08
 */
public class UnmodifiableMapDemo {


    public static void main(String[] args) {
        Map<Integer, StringBuilder> map = new HashMap<Integer, StringBuilder>();
        map.put(1, new StringBuilder("c1"));
        map.put(2, new StringBuilder("c2"));
        map.put(3, new StringBuilder("c3"));

        Map<Integer, StringBuilder> unmodifiableMap = Collections.unmodifiableMap(map);
        //这时候如果再往unmodifiableMap中添加元素，会发生错误
        try {

            unmodifiableMap.put(4, new StringBuilder("c4"));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("ERROR " + e.getMessage());
        }
        //但是  由于 StringBuilder 的属性是可以变的 , so 底下正常执行
        unmodifiableMap.get(3).append("new");
        System.out.println(unmodifiableMap.get(3));

    }
}
